package cn.slahser.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import cn.slahser.R;
import cn.slahser.utils.LoginClientTCP;
import cn.slahser.utils.SlahserMetaData;

public class LoginActivity extends Activity {

	protected Button loginbutton = null;
	protected Button regbutton = null;
	protected EditText username = null;
	protected EditText password = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		loginbutton = (Button) findViewById(R.id.login_button_submit);
		regbutton = (Button) findViewById(R.id.login_button_reg);
		username = (EditText) findViewById(R.id.login_edittext_userid);
		password = (EditText) findViewById(R.id.login_edittext_password);
		loginbutton.setOnClickListener(new LoginButtonOnClickListener());
		regbutton.setOnClickListener(new RegButtonOnClickListener());

		String userid = this.getIntent().getStringExtra("userid");
		if (userid != null) {
			username.setText(userid);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add("set server info").setOnMenuItemClickListener(
				new SettingOnMenuItemClickListener());
		return super.onCreateOptionsMenu(menu);
	}

	class LoginButtonOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			try {
				String message = LoginClientTCP.login(username.getText()
						.toString(), password.getText().toString());
				if (message.equalsIgnoreCase("ok")) {
					Toast.makeText(LoginActivity.this, "login successfull！",
							Toast.LENGTH_LONG).show();
					startActivity(new Intent(LoginActivity.this,
							MainActivity.class));
					finish();

				} else if (message.equalsIgnoreCase("error")) {
					Toast.makeText(LoginActivity.this,
							"new user ,have not active！", Toast.LENGTH_LONG)
							.show();
				} else if (message.equalsIgnoreCase("psswordError")) {
					Toast.makeText(LoginActivity.this, "wrong passwprd！",
							Toast.LENGTH_LONG).show();
					password.setText("");
					password.requestFocus();
				} else if (message.equalsIgnoreCase("notUser")) {
					Toast.makeText(LoginActivity.this, "no such user！",
							Toast.LENGTH_LONG).show();
					username.setText("");
					username.requestFocus();
					password.setText("");
				}
			} catch (Exception e) {
				Toast.makeText(LoginActivity.this, "connect failed！",
						Toast.LENGTH_LONG).show();
			}

		}

	}

	class RegButtonOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			startActivity(new Intent(LoginActivity.this, RegActivity.class));
			finish();
		}

	}

	class SettingOnMenuItemClickListener implements OnMenuItemClickListener {
		@Override
		public boolean onMenuItemClick(MenuItem item) {
			AlertDialog.Builder buil = new AlertDialog.Builder(
					LoginActivity.this);
			buil.setTitle("set server info");
			LinearLayout line = (LinearLayout) LayoutInflater.from(
					LoginActivity.this)
					.inflate(R.layout.activity_setting, null);

			final EditText ip = (EditText) line
					.findViewById(R.id.setting_edittext_ip);
			final EditText servePort = (EditText) line
					.findViewById(R.id.setting_edittext_serveport);
			final EditText actionPort = (EditText) line
					.findViewById(R.id.setting_edittext_actionport);
			final EditText filePort = (EditText) line
					.findViewById(R.id.setting_edittext_fileport);
			ip.setText(SlahserMetaData.IP);
			servePort.setText(SlahserMetaData.SERVEPORT + "");
			actionPort.setText(SlahserMetaData.ACTIONPORT + "");
			filePort.setText(SlahserMetaData.FILEPORT + "");

			buil.setView(line);

			buil.setNeutralButton("Setting",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							try {
								SlahserMetaData.IP = ip.getText().toString();
								SlahserMetaData.SERVEPORT = Integer
										.parseInt(servePort.getText()
												.toString());
								SlahserMetaData.ACTIONPORT = Integer
										.parseInt(actionPort.getText()
												.toString());
								SlahserMetaData.FILEPORT = Integer
										.parseInt(filePort.getText().toString());
								Toast.makeText(LoginActivity.this, "succed！",
										Toast.LENGTH_LONG).show();
							} catch (Exception e) {
								Toast.makeText(LoginActivity.this, "failed！",
										Toast.LENGTH_LONG).show();
							}
						}
					});
			buil.setNegativeButton("close", null);
			buil.create().show();
			return false;
		}

	}
}